package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.RentDao;
import com.dc.model.Rent;
import com.dc.service.RentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RentServiceImpl implements RentService {
    @Autowired
    RentDao rentDao;
    public BaseModel selectPageRent(Integer curr_page, Integer page_size, BaseModel baseModel,Integer rent_id, String client_name, String flower_name) {
        // 编写业务
        PageHelper.startPage(curr_page,page_size);
        // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
        List<Rent> users=rentDao.selectPageRent(rent_id,client_name,flower_name);
        PageInfo pageInfo=new PageInfo(users,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询购入信息成功！");
        baseModel.setData(pageInfo);
        return baseModel;
    }
    // 新增租出的接口
    public BaseModel addRent(Rent rent, BaseModel baseModel) throws Exception {
        //调用新增租出的dao
        Integer result=rentDao.insertRent(rent);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增购入信息成功！");
        return baseModel;
    }

    // 修改租出的接口
    public BaseModel updateRent(Rent rent, BaseModel baseModel) throws Exception {
        Integer result=rentDao.updateRent(rent);
        if(result<=0){
            throw  new BusinessException(1008,"修改购入信息失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改购入信息成功！");
        return baseModel;
    }

    // 删除租出的接口
    public BaseModel deleteRent(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=rentDao.deleteRent(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个租出信息失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个租出信息成功！");
        return baseModel;
    }

    // 根据id查询租出信息的接口
    public BaseModel selectRentById(Integer rent_id,BaseModel baseModel) throws Exception{
        List<Rent> rentList = rentDao.selectRentById(rent_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个租出信息成功");
        baseModel.setData(rentList);
        return baseModel;
    }
}
